package com.atsmart.app.activity;

import com.atsmart.app.R;
import com.atsmart.app.utils.PrefsUtils;
import com.atsmart.app.utils.StringUtil;
import com.google.json.Gson;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

public class NavActivity extends BaseActivity {
	private Gson gson;
	private Context context;
	public static NavActivity instance = null;
	private SlidingMenu slidingMenu;
	private ImageView top_btn;
	private View menuView;
	private TextView task, report, client, product, msg, history, upload,
			version;
	private LinearLayout task_layout, report_layout, client_layout,
			product_layout, msg_layout, history_layout, upload_layout,
			version_layout;
	private TextView title;
	private LinearLayout content;
	private int screenwidth,screenheight;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.content_layout);
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		screenheight = metrics.heightPixels;
		screenwidth = metrics.widthPixels;
		initAttribute();
		
		slidingMenu = new SlidingMenu(context);
		slidingMenu.mViewBehind.setLayoutParams(new LayoutParams(200, LayoutParams.MATCH_PARENT));
		slidingMenu.setMode(SlidingMenu.LEFT);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
		slidingMenu.setShadowDrawable(R.drawable.shadow);
		slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		slidingMenu.setFadeDegree(0.35f);
		slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		slidingMenu.setMenu(menuView);
//		slidingMenu.openMenu();
		clickEvent();
	}

	/**控件初始化*/
	private void initAttribute() {
		instance = this;
		context = this;
		gson = new Gson();
		menuView = LayoutInflater.from(this).inflate(R.layout.menu_layout, null);
		title = (TextView) findViewById(R.id.top_title);
		content = (LinearLayout) findViewById(R.id.content_layout);
		top_btn = (ImageView) findViewById(R.id.top_image_btn);
		task = (TextView) menuView.findViewById(R.id.task_text);
		report = (TextView) menuView.findViewById(R.id.report_text);
		client = (TextView) menuView.findViewById(R.id.client_text);
		product = (TextView) menuView.findViewById(R.id.product_text);
		msg = (TextView) menuView.findViewById(R.id.msg_text);
		history = (TextView) menuView.findViewById(R.id.history_text);
		upload = (TextView) menuView.findViewById(R.id.upload_text);
		version = (TextView) menuView.findViewById(R.id.version_text);

		task_layout = (LinearLayout) menuView.findViewById(R.id.layout1);
		report_layout = (LinearLayout) menuView.findViewById(R.id.layout2);
		client_layout = (LinearLayout) menuView.findViewById(R.id.layout3);
		product_layout = (LinearLayout) menuView.findViewById(R.id.layout4);
		msg_layout = (LinearLayout) menuView.findViewById(R.id.layout5);
		history_layout = (LinearLayout) menuView.findViewById(R.id.layout6);
		upload_layout = (LinearLayout) menuView.findViewById(R.id.layout7);
		version_layout = (LinearLayout) menuView.findViewById(R.id.layout8);
	}

	/**点击事件*/
	private void clickEvent() {
		top_btn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (slidingMenu.isMenuShowing()) {
					slidingMenu.closeMenu();
				} else {
					slidingMenu.openMenu();
				}
			}
		});
		task_layout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setChildBg(1);
			}
		});
		report_layout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setChildBg(2);
			}
		});
		client_layout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setChildBg(3);
			}
		});
		product_layout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setChildBg(4);
			}
		});
		msg_layout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setChildBg(5);
			}
		});
		history_layout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setChildBg(6);
			}
		});
		upload_layout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setChildBg(7);
			}
		});
		version_layout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setChildBg(8);
			}
		});
	}

	/**内容顶部标题*/
	private void setTitle(String text) {
		title.setText(text);
	}

	/**点击滑动菜单item时背景切换*/
	private void setChildBg(int index) {
		if (index == 1) {
			setTitle("任务");
			PrefsUtils.setLastindex("1");
			task_layout.setBackgroundResource(R.drawable.icon_child_press);
		} else {
			task_layout.setBackgroundResource(R.drawable.icon_child_back);
		}
		if (index == 2) {
			setTitle("汇报");
			PrefsUtils.setLastindex("2");
			report_layout.setBackgroundResource(R.drawable.icon_child_press);
		} else {
			report_layout.setBackgroundResource(R.drawable.icon_child_back);
		}
		if (index == 3) {
			setTitle("客户");
			PrefsUtils.setLastindex("3");
			client_layout.setBackgroundResource(R.drawable.icon_child_press);
		} else {
			client_layout.setBackgroundResource(R.drawable.icon_child_back);
		}
		if (index == 4) {
			setTitle("产品");
			PrefsUtils.setLastindex("4");
			product_layout.setBackgroundResource(R.drawable.icon_child_press);
		} else {
			product_layout.setBackgroundResource(R.drawable.icon_child_back);
		}
		if (index == 5) {
			setTitle("信息");
			PrefsUtils.setLastindex("5");
			msg_layout.setBackgroundResource(R.drawable.icon_child_press);
		} else {
			msg_layout.setBackgroundResource(R.drawable.icon_child_back);

		}
		if (index == 6) {
			setTitle("历史");
			PrefsUtils.setLastindex("6");
			history_layout.setBackgroundResource(R.drawable.icon_child_press);
		} else {
			history_layout.setBackgroundResource(R.drawable.icon_child_back);
		}
		if (index == 7) {
			setTitle("上传");
			PrefsUtils.setLastindex("7");
			upload_layout.setBackgroundResource(R.drawable.icon_child_press);
		} else {
			upload_layout.setBackgroundResource(R.drawable.icon_child_back);
		}
		if (index == 8) {
			setTitle("版本");
			PrefsUtils.setLastindex("8");
			version_layout.setBackgroundResource(R.drawable.icon_child_press);
		} else {
			version_layout.setBackgroundResource(R.drawable.icon_child_back);
		}
	}

	@Override
	protected void onResume() {
		if(!StringUtil.isEmptyOrNull(PrefsUtils.getLastIndex())){
			setChildBg(Integer.parseInt(PrefsUtils.getLastIndex()));
			slidingMenu.closeMenu();
		}else{
			slidingMenu.openMenu();
			setChildBg(1);
		}
		super.onResume();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		return super.onKeyDown(keyCode, event);
	}
}